package com.wxq.maliao;


import javazoom.jl.decoder.JavaLayerException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class MyFrame extends JFrame implements KeyListener ,Runnable{

    // 存储所有背景
    private List<BackGround> allBg = new ArrayList<>();
    // 当前场景
    private BackGround nowBg = new BackGround();
    // 双缓存
    private Image offScreenImage = null;
    // 马里奥指针
    private Maliao maliao = new Maliao();

    // 实现马里奥移动线程
    private Thread thread = new Thread(this);

    public MyFrame() throws HeadlessException {
        // 窗口大小
        this.setSize(800, 600);
        // 窗口剧中显示
        this.setLocationRelativeTo(null);
        // 窗口可见性
        this.setVisible(true);
        // 设置窗口关闭键，结束程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口大小不可变
        this.setResizable(false);
        // 向窗口对象添加键盘监听器
        this.addKeyListener(this);
        // 设置窗口名称
        this.setTitle("超级玛丽");
        // 初始化图片
        StaticValue.init();
        // 初始化马里奥
        maliao = new Maliao(10,355);


        for (int i = 1; i <= 3; i++) {
            allBg.add(new BackGround(i, i == 3 ? true : false));

        }
        nowBg = allBg.get(0);

        maliao.setBackGround(nowBg);

        repaint();

        thread.start();
        try {
            new Music();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (JavaLayerException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void paint(Graphics g) {
        if (null == offScreenImage) {
            offScreenImage = createImage(800, 600);
        }
        Graphics graphics = offScreenImage.getGraphics();
        graphics.fillRect(0, 0, 800, 600);
        // 绘制背景
        graphics.drawImage(nowBg.getBgImage(), 0, 0, this);

        // 绘制障碍物
        for (Obstacle obstacle : nowBg.getObstacleList()) {
            graphics.drawImage(obstacle.getShow(), obstacle.getX(), obstacle.getY(), this);
        }

        // 绘制城堡
        graphics.drawImage(nowBg.getTower(), 620, 270, this);

        // 绘制旗杆
        graphics.drawImage(nowBg.getGan(), 500, 220, this);
        // 绘制马里奥
        graphics.drawImage(maliao.getShow(),maliao.getX(),maliao.getY(),this);

        // 绘制敌人
        for (Enemy e : nowBg.getEnemieList()) {
            graphics.drawImage(e.getShow(),e.getX(),e.getY(),this);
        }

        Color c = graphics.getColor();
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("黑体",Font.BOLD,25));
        graphics.drawString("当前的分数为："+maliao.getScore(),300,100);
        graphics.setColor(c);

        // 渲染窗口
        g.drawImage(offScreenImage, 0, 0, this);
        



    }


    public static void main(String[] args) {
        MyFrame frame = new MyFrame();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // 向右移动
        if (e.getKeyCode() == 39) {
            maliao.rightMove();
        }

        // 向左移动
        if (e.getKeyCode() == 37) {
            maliao.leftMove();
        }

        // 跳跃
        if (e.getKeyCode() == 38) {
            maliao.jump();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // 向左停止
        if (e.getKeyCode() == 37) {
            maliao.leftStop();
        }
        // 向右停止
        if (e.getKeyCode() == 39) {
            maliao.rightStop();
        }
    }

    @Override
    public void run() {
        while (true) {
            repaint();
            try {
                TimeUnit.MILLISECONDS.sleep(50);

                if (maliao.getX() >= 775) {
                    nowBg = allBg. get(nowBg.getSort());
                    maliao.setBackGround(nowBg);
                    maliao.setX(10);
                    maliao.setY(355);
                }
                // 判断马里奥是否死亡
                if (maliao.isDeath()) {
                    JOptionPane.showMessageDialog(this,"GAME OVER");
                    System.exit(0);
                }

                // 判断游戏是否结束
                if (maliao.isOK()) {
                    JOptionPane.showMessageDialog(this,"恭喜成功通关");
                    System.exit(0);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



}
